package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.AdminController;
import com.zhangzeqin.code.entity.WordBook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.sql.SQLException;

public class WordBookView extends JFrame{
    public static void main(String[] args) {
        new WordBookView();
    }
    JLabel nameLabel=new JLabel("菜单界面",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    JButton learnButton=new JButton("背诵单词");
    JButton recordButton=new JButton("学习记录");
    JButton myButton=new JButton("个人信息");
    SystemTray systemTray;
    TrayIcon trayIcon;



    WordBookHandler wordBookHandler;

    public WordBookView(){
        super("扇贝单词项目");
        wordBookHandler=new WordBookHandler(this);

        //获取内容面板
        Container contentPane=getContentPane();
        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(0,80));
        Font  centerFont=new Font("楷体",Font.PLAIN,20);

        learnButton.setFont(centerFont);
        recordButton.setFont(centerFont);
        myButton.setFont(centerFont);

        //增加按钮事件
        learnButton.addActionListener(wordBookHandler);
        recordButton.addActionListener(wordBookHandler);
        myButton.addActionListener(wordBookHandler);
        centerPanel.add(learnButton);
        centerPanel.add(recordButton);
        centerPanel.add(myButton);

        //弹簧布局
        layout(contentPane);

        //增加系统托盘
        addTray();


        //自定义图标
        URL imgUrl=LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600,400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);



    }



    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局learnButton
        Spring childWidth=Spring.sum(Spring.width(learnButton), Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,learnButton,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,learnButton,20,SpringLayout.NORTH,centerPanel);
        //recordButton
        springLayout.putConstraint(SpringLayout.WEST, recordButton,0,SpringLayout.WEST,learnButton);
        springLayout.putConstraint(SpringLayout.NORTH,recordButton,20,SpringLayout.SOUTH,learnButton);
        //myButton
        springLayout.putConstraint(SpringLayout.WEST,myButton,0,SpringLayout.WEST,recordButton);
        springLayout.putConstraint(SpringLayout.NORTH,myButton,20,SpringLayout.SOUTH,recordButton);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    /**
     * 增加系统托盘
     */
    private void addTray() {
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();
            URL imgUrl=LoginView.class.getClassLoader().getResource("tray.png");
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片大小自动缩放
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    WordBookView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        WordBookView.this.setExtendedState(JFrame.NORMAL);
                    }
                    WordBookView.this.setVisible(true);
                }
            });
        }
    }


}
